import {defineStore} from "pinia";
import {reactive, ref, toRaw} from "vue";

export const userOrderStore = defineStore('order', () => {
    const orderInfo = reactive({
        shopId:"",
        orderId:"",
        shopName:"",
        orderTotal:"",
        shopLineItems:[],

    })
    function setOrderInfo(orderInfo) {
        this.orderInfo.shopName = orderInfo.shopName
        this.orderInfo.shopId = orderInfo.shopId
        this.orderInfo.orderId = orderInfo.id
        this.orderInfo.shopLineItems = orderInfo.lineItemDTOS
    }
    function setShopId(shopId) {
        this.orderInfo.shopId = shopId
    }
    function updateLineItem(lineItem){
        const index = this.orderInfo.shopLineItems.findIndex(item=>(item.shopItemId || item.id) === lineItem.id )
        if (index !== -1){
            this.orderInfo.shopLineItems[index].quantity = lineItem.quantity
        }else {
            this.orderInfo.shopLineItems.push(
                {
                    id:"",
                    shopItemId:lineItem.id,
                    quantity:lineItem.quantity,
                    shopItemPrice:lineItem.shopItemPrice,
                    itemImg:lineItem.itemImg,
                    itemName:lineItem.itemName
                }
            )
        }
    }
    function setTotal(price) {
        this.orderInfo.orderTotal = price
    }
    function getLineItems(){
        return this.orderInfo.shopLineItems
    }
    function getNoneQuantityLineItems(){
        return this.orderInfo.shopLineItems.filter(item => item.quantity !== 0);
    }
    return { orderInfo, setOrderInfo,setTotal,updateLineItem,getLineItems,setShopId,getNoneQuantityLineItems }
})